/*
 * @Author: lei
 * @Date: 2021-08-03 21:39:45
 * @LastEditTime: 2021-08-03 22:05:58
 * @LastEditors: Please set LastEditors
 * @Description: 内存对齐测试
 * @FilePath: /code/memory_alignment.cpp
 */

//#pragma pack(2)//加上这行，test1函数输出结果：sizeof(a)= 16
#include <iostream>
#include <cstddef>
using namespace std;

struct A
{
    char c;
    int a;
    double d;
    char c1;
};

void test1()
{
    A a;
    cout<<"sizeof(a)= "<<sizeof(a)<<endl;
}

void test2()
{
    cout<<"offsetof(A,c)= "<<offsetof(A,c)<<endl;
    cout<<"offsetof(A,a)= "<<offsetof(A,a)<<endl;
    cout<<"offsetof(A,d)= "<<offsetof(A,d)<<endl;
    cout<<"offsetof(A,c1)= "<<offsetof(A,c1)<<endl;
}

int main()
{
    test1();
    test2();
    return 0;
}
//test1函数输出结果： sizeof(a)= 24
/*
test2函数输出结果： 
offsetof(A,c)= 0
offsetof(A,a)= 4
offsetof(A,d)= 8
offsetof(A,c1)= 16
*/